package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.DormCheck;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 寝室考勤表dao
 * @author Administrator
 *
 */
public class DormCheckDaoImp implements CommnonDao<DormCheck>{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(DormCheck value) throws Exception {
		String sql="INSERT INTO t_dorm_check(dorm_id,dorm_state,create_time,";
		sql+="check_term,check_month) VALUES(?,?,?,?,?)";
		long Number=JdbcUtil.insertAuto(sql,value.getDormId(),
				value.getDormState(),
				value.getCreateTime(),
				value.getCheckTerm(),
				value.getCheckMonth());
		return Number;
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(DormCheck value) throws Exception {
		String sql="UPDATE t_dorm_check SET dorm_id=?,dorm_state=?,create_time=?,check_term=?,check_month=?";
		sql+=" WHERE dorm_check_id=?";
		int Number = JdbcUtil.update(sql,value.getDormId(),
				value.getDormState(),
				value.getCreateTime(),
				value.getCheckTerm(),
				value.getCheckMonth(),
				value.getDormCheckId());
		return Number;
	}
	/**
	 * 删除内容
	 * 寝室表t_dorm
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(DormCheck value) throws Exception {
		return 0;
	}
	/**
	 * 查找所有的内容
	 * 寝室表t_dorm
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<DormCheck> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT  tdc.dorm_id,tdc.dorm_state,tdc.create_time,tdc.check_term,tdc.check_month,d.dorm_name ";
		sql+="FROM  t_dorm_check tdc,t_dorm d limit ?,?";
		return JdbcUtil.query(DormCheck.class, sql,(pageNo-1)*pageSize,pageSize);
	}
	/**
	 * 查找单一实体
	 * @param Key
	 * @throws Exception
	 */
	@Override
	public DormCheck findByKey(Object Key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 查询某班总共有多少宿舍考勤
	 * @param classId
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public long getCount(Integer classId,Integer pageSize) throws Exception{
		String sql="SELECT  COUNT(*) FROM  t_dorm_check tdc,t_class_dorm cd,t_dorm d WHERE tdc.dorm_id=d.dorm_id AND cd.dorm_id=d.dorm_id AND cd.class_id=?";
		long count =  (long) JdbcUtil.uniqueValue(sql, 1, classId);
		return count%pageSize==0?count/pageSize:count/pageSize+1;
	}
	
	/**
	 * 用于导出excel
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String  getDormName(Integer id) throws Exception{
		String sql="select stu_dorm_name from t_student where stu_id=?";
		return (String) JdbcUtil.uniqueValue(sql, 1, id);
	} 
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DormCheck> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="SELECT  tdc.dorm_id,tdc.dorm_state,tdc.create_time,tdc.check_term,tdc.check_month,d.dorm_name ";
		sql+="FROM  t_dorm_check tdc,t_dorm d  ";
		sql+="WHERE d.dorm_id=tdc.dorm_id and d.dorm_name= ? LIMIT ?,?";
		return JdbcUtil.query(DormCheck.class, sql, key, (pageNo-1)*pageSize, pageSize);
	}
	
	/**
	 * 按班级查询
	 * @param classId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<DormCheck> findAll(Integer classId, int pageNo, int pageSize) throws Exception {
		String sql="SELECT tdc.dorm_id,tdc.dorm_state,tdc.create_time,tdc.check_term,"
				+ "tdc.check_month,d.dorm_name "
				+ "FROM  t_dorm_check tdc,t_class_dorm cd,t_dorm d "
				+ "WHERE tdc.dorm_id=d.dorm_id AND cd.dorm_id=d.dorm_id "
				+ "AND cd.class_id=? ORDER BY dorm_check_id DESC LIMIT ?,? ";
		return JdbcUtil.query(DormCheck.class, sql,classId,(pageNo-1)*pageSize,pageSize);
	}

	/**
	 * 
	 * @param dormName
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public long getPages(String dormName, Integer pageSize) throws Exception{
		String sql="SELECT count(*) FROM  t_dorm_check tdc,t_dorm d  ";
		sql+="WHERE d.dorm_id = tdc.dorm_id and d.dorm_name=?";
		long count = (Long) JdbcUtil.uniqueValue(sql, 1, dormName);
		long pages = pageSize.longValue();
		return count%pages==0?count/pages:count/pages+1;
	}
}
